package com.lcq.study.javabase;

/**
 * @ClassName StringTest
 * @Description
 * @Author lichuanqi
 * @Date 2021/11/18 16:23
 * @Version 1.0
 **/
public class StringTest {
    public static void main(String[] args) {
        //test2();

        String a = "";
        String b = "1";
        String c = "1,2";

//        String[] split = a.split(",");
//        System.out.println(split.length);
//        String[] split1 = b.split(",");
//        System.out.println(split1.length);
//        String[] split2 = c.split(",");
//        System.out.println(split2.length);

//        int i = c.indexOf(",");
//        System.out.println(i);
//        System.out.println(c.substring(i+1));


        System.out.println(countSub(a,","));
        System.out.println(countSub(b,","));
        System.out.println(countSub(c,","));
    }

    private static int countSub(String s, String subs){
        int i = -1;
        int count = 0;
        while((i = s.indexOf(subs))!=-1){
            s = s.substring(i+1);
            count++;
        }
        return count;
    }

    private static void test1() {
        //这个等于两步 String s = "ss"; String ss = new String(s);
        //因此这里会构造两个对象
        String ss = new String("ss");
        //这里仅仅是个浅拷贝，新构建了一个String对象，
        //但是String对象中的char数组只是常量池中char数组的引用。
        String ss1 = new String("ss");
        String ss2 = new String("ss");
        //新的String对象都不会构建，直接获取常量池中的
        String ss3 = "ss";
        System.out.println(ss==ss1);
        System.out.println(ss1==ss2);
        System.out.println(ss2==ss);
        System.out.println(ss2==ss3);
        System.out.println(ss==ss3);
        System.out.println(ss1==ss3);
    }

    private static void test2() {
        String sa = new String("lcq") + new String("dsg");
        String sa1 = new String("lcqdsg");
//        String s = new String("lcq");
//        String a = new String("dsg");
//        //String sa1 = new String("sa");
//        String sa2 = "lcqdsg";
//        String s1 = "lcq";
//        String a1 = "dsg";
//        System.out.println(sa==sa2);
    }
}
